package vn.com.vega.music.view.adapter;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.Appinstant;
import vn.com.vega.music.objects.Artist;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ArtistInfoAdapter extends BaseAdapter {
	private static final int TYPE_INFO = 0;
	private static final int TYPE_FILLTER = 1;
	private static final int TOTAL_TYPE = 2;

	public static final int SONG_FILLTER = 10;
	public static final int VIDEO_FILLTER = 11;
	public static final int ALBUM_FILLTER = 12;
	public int currentFillter = SONG_FILLTER;

	public static final String SONG_FILLTER_STRING = "Bài hát";
	public static final String VIDEO_FILLTER_STRING = "Video";
	public static final String AlBUM_FILLTER_STRING = "Album";

	private Artist mArtist;
	private LayoutInflater inflater;
	private Appinstant mApp;
	private showAndSelectFillter showAndSelectFillterListener;

	public ArtistInfoAdapter(Artist artist, Context ctx,
			showAndSelectFillter listener) {
		mArtist = artist;
		mApp = (Appinstant) ctx.getApplicationContext();
		inflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		showAndSelectFillterListener = listener;
	}

	public void setFillter(int fillter) {
		if (fillter != currentFillter) {
			currentFillter = fillter;
			notifyDataSetChanged();
		}
	}

	static public String getFillterName(int fillter) {
		switch (fillter) {
		case SONG_FILLTER:
			return SONG_FILLTER_STRING;
		case VIDEO_FILLTER:
			return VIDEO_FILLTER_STRING;
		case ALBUM_FILLTER:
			return AlBUM_FILLTER_STRING;
		default:
			return SONG_FILLTER_STRING;
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		int viewType = getItemViewType(position);
		if (viewType == TYPE_INFO) {
			ViewAtistHolder holder;
			// if (convertView == null) {
			convertView = inflater.inflate(R.layout.artist_detail_item_artist,
					null);
			holder = new ViewAtistHolder();
			holder.artistThumb = (ImageView) convertView
					.findViewById(R.id.imv_album_thumb);
			holder.atistName = (TextView) convertView
					.findViewById(R.id.tv_artist_name);
//			holder.atistDes = (TextView) convertView
//					.findViewById(R.id.tv_album_des);
			holder.songCount = (TextView) convertView.findViewById(R.id.tv_song_count);
			holder.videoCount = (TextView) convertView.findViewById(R.id.tv_video_count);
			holder.albumCount = (TextView) convertView.findViewById(R.id.tv_album_count);
			convertView.setTag(holder);
			// } else {
			// holder = (ViewAtistHolder) convertView.getTag();
			// }
			holder.atistName.setText(mArtist.name);
			holder.songCount.setText(mArtist.songCount + " bài hát");
			holder.videoCount.setText(mArtist.videoCount + " video");
			holder.albumCount.setText(mArtist.albumCount + " album");
//			holder.atistDes.setText(mArtist.description);
			mApp.mImageLoader.download(mArtist.imageUrl, holder.artistThumb);
			return convertView;

		} else {
			ViewAtistHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.atist_fillter, null);
				holder = new ViewAtistHolder();
				holder.atistFiltter = (TextView) convertView
						.findViewById(R.id.tv_search_fillter_category);
				holder.fillterView = convertView
						.findViewById(R.id.tv_type_fillter);
				holder.fillterView
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								// TODO Auto-generated method stub
								showAndSelectFillterListener.showFillter();
							}
						});
				convertView.setTag(holder);
			} else {
				holder = (ViewAtistHolder) convertView.getTag();
			}
			try {
				// Some time don't why have error here :)
				holder.atistFiltter.setText(getFillterName(currentFillter));
			} catch (Exception e) {
				// TODO: handle exception
			}
			return convertView;
		}
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (position == 0) {
			return TYPE_INFO;
		} else {
			return TYPE_FILLTER;
		}
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return TOTAL_TYPE;
	}

	static class ViewAtistHolder {
		ImageView artistThumb;
		TextView atistName;
		TextView songCount;
		TextView videoCount;
		TextView albumCount;
		TextView atistFiltter;
		View fillterView;
	}

	public interface showAndSelectFillter {
		public void showFillter();

		public void selectFillter(int fillter);
	}

}
